package com.zx.zxapibackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.zxapibackend.model.vo.AllInterfaceInfoVo;
import com.zx.zxapicommon.common.BaseResponse;
import com.zx.zxapicommon.model.enums.ErrorCode;
import com.zx.zxapicommon.exception.BusinessException;
import com.zx.zxapibackend.exception.ThrowUtils;
import com.zx.zxapibackend.mapper.InterfaceInfoMapper;
import com.zx.zxapibackend.model.dto.interfaceInfo.InterfaceInfoQueryRequest;
import com.zx.zxapibackend.service.InterfaceInfoService;
import com.zx.zxapicommon.model.entity.InterfaceInfo;
import com.zx.zxapicommon.utils.ResultUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Bro
 * @description 针对表【interface_info(接口信息)】的数据库操作Service实现
 * @createDate 2023-04-13 10:41:14
 */
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
        implements InterfaceInfoService {

    @Resource
    private InterfaceInfoMapper interfaceInfoMapper;

    @Override
    public void validInterfaceInfo(InterfaceInfo interfaceInfo, boolean add) {
        if (interfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        String name = interfaceInfo.getName();
        String description = interfaceInfo.getDescription();
        String url = interfaceInfo.getUrl();
        String requestHeader = interfaceInfo.getRequestHeader();
        String responseHeader = interfaceInfo.getResponseHeader();

        String method = interfaceInfo.getMethod();


        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(name, description, url, requestHeader, responseHeader, method), ErrorCode.PARAMS_ERROR);
        }
        // 有参数则校验
        if (StringUtils.isNotBlank(name) && name.length() > 80) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "接口名字过长");
        }
        if (StringUtils.isNotBlank(description) && description.length() > 8192) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "接口描述过长");
        }
    }



    /**
     * 管理员 —— 分页获取所有的接口信息(包括已下线)
     *
     * @param interfaceInfoQueryRequest
     * @param request
     * @return
     */
    @Override
    public BaseResponse<Page<AllInterfaceInfoVo>> getAllInterfaceInfoPage(InterfaceInfoQueryRequest interfaceInfoQueryRequest, HttpServletRequest request) {
        long current = interfaceInfoQueryRequest.getCurrent();
        long pageSize = interfaceInfoQueryRequest.getPageSize();
        //限制爬虫
        if (pageSize > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<AllInterfaceInfoVo> allInterfaceInfoVoPage =
                interfaceInfoMapper.selectAllPage(new Page<>(current, pageSize), interfaceInfoQueryRequest);

        return ResultUtils.success(allInterfaceInfoVoPage);
    }

    /**
     * 分页获取已上线的接口列表
     *
     * @param interfaceInfoQueryRequest
     * @return
     */
    @Override
    public BaseResponse<Page<AllInterfaceInfoVo>> listInterfaceInfoByPage(InterfaceInfoQueryRequest interfaceInfoQueryRequest) {
        long current = interfaceInfoQueryRequest.getCurrent();
        long pageSize = interfaceInfoQueryRequest.getPageSize();
        Page<AllInterfaceInfoVo> allInterfaceInfoVoPage = interfaceInfoMapper.selectOnLinePage(new Page<>(current, pageSize), interfaceInfoQueryRequest);
        return ResultUtils.success(allInterfaceInfoVoPage);
    }


}




